package main.java.common;

import java.awt.Color;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JPanel;

import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.factories.FormFactory;

import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreePath;

import main.java.ERMDE.controllers.IProjectController;
import main.java.ERMDE.views.IProjectView;

public class ProjectView extends JPanel implements IProjectView {

    private static final long serialVersionUID = 1L;
    private JTree tree;
    private JButton btnOpen;
    private JButton btnCreate;
    private IProjectController projectController;
    private JButton btnValidate;
    private ProjectCreateView projectCreateView;
    private ProjectOpenView projectOpenView;
    private ProjectConvertView projectConvertView;
    private ProjectTreeView projectTreeView;
	private JButton btnConvert;
    
    /**
     * Create the panel.
     */
    public ProjectView() {
	setLayout(new FormLayout(
		new ColumnSpec[] { FormFactory.RELATED_GAP_COLSPEC,
			ColumnSpec.decode("default:grow"),
			FormFactory.RELATED_GAP_COLSPEC,
			FormFactory.DEFAULT_COLSPEC,
			FormFactory.RELATED_GAP_COLSPEC,
			FormFactory.DEFAULT_COLSPEC,
			FormFactory.RELATED_GAP_COLSPEC,
			FormFactory.DEFAULT_COLSPEC,
			FormFactory.RELATED_GAP_COLSPEC,
			FormFactory.DEFAULT_COLSPEC,
			FormFactory.RELATED_GAP_COLSPEC,
			FormFactory.DEFAULT_COLSPEC,
			FormFactory.RELATED_GAP_COLSPEC,
			FormFactory.DEFAULT_COLSPEC, }, new RowSpec[] {
			FormFactory.RELATED_GAP_ROWSPEC,
			FormFactory.DEFAULT_ROWSPEC,
			FormFactory.RELATED_GAP_ROWSPEC,
			RowSpec.decode("default:grow"), }));

	this.btnCreate = new JButton("Create");
	this.add(this.btnCreate, "2, 2");
	
	this.btnOpen = new JButton("Open");
	this.add(this.btnOpen, "4, 2");

//	this.btnValidate = new JButton("Validate");
//	this.add(btnValidate, "6, 2");
//	
	this.btnConvert = new JButton("Convert");
	this.add(btnConvert, "6, 2");
	
	this.tree = new JTree();
	tree.setModel(null);
	JScrollPane treeScrollPane = new JScrollPane(this.tree);
	this.add(treeScrollPane, "2, 4, 9, 1, fill, fill");

	projectCreateView = new ProjectCreateView();
	projectOpenView = new ProjectOpenView();
	projectConvertView = new ProjectConvertView();
	projectTreeView = new ProjectTreeView(tree);
	
    }
 
    @Override
    public void setController(IProjectController controller) {
	this.projectController = controller;
	projectTreeView.setController(projectController);
	projectOpenView.setController(projectController);
	projectOpenView.setTree(tree);	
	projectCreateView.setController(projectController);
	projectCreateView.setTree(tree);
	projectConvertView.setController(projectController);
	projectConvertView.setTree(tree);	
	
	this.btnCreate.addMouseListener(new MouseAdapter() {
   	    @Override
	    public void mouseClicked(MouseEvent e) {
		projectCreateView.createDialog(btnCreate);
		initializeTreeDiagram();
	    }
	});

	this.btnOpen.addMouseListener(new MouseAdapter() {
	    @Override
	    public void mouseClicked(MouseEvent e) {
		projectOpenView.openDialog(btnOpen);
		initializeTreeDiagram();
	    }
	});

//	this.btnValidate.addMouseListener(new MouseAdapter() {
//
//	    @Override
//	    public void mouseClicked(MouseEvent e) {
//		
//		projectOpenView.validateDialog(btnOpen);
//
//	    }
//	});
	
	this.btnConvert.addMouseListener(new MouseAdapter() {
	    @Override
	    public void mouseClicked(MouseEvent e) {
		projectConvertView.openDialog(btnConvert);
		initializeTreeDiagram();
	    }
	});
    }
    
    private void initializeTreeDiagram() {
	projectTreeView.initializeTree();
	projectTreeView.initializeShowDiagram();
	projectTreeView.initializeEditDiagram();
    }


    @SuppressWarnings("serial")
    @Override
    public void expandPath(final TreePath path) {
	tree.expandPath(path);
	tree.setCellRenderer(new DefaultTreeCellRenderer() {
	    @Override
	    public Component getTreeCellRendererComponent(JTree pTree,
		    Object pValue, boolean pIsSelected, boolean pIsExpanded,
		    boolean pIsLeaf, int pRow, boolean pHasFocus) {
		// Component c = super.getTreeCellRendererComponent(tree, value,
		// selected, expanded, isLeaf, row, focused);
		DefaultMutableTreeNode node = (DefaultMutableTreeNode) pValue;
		super.getTreeCellRendererComponent(pTree, pValue, pIsSelected,
			pIsExpanded, pIsLeaf, pRow, pHasFocus);
		if (node instanceof main.java.FSMDE.infrastructure.visual.DiagramTreeNode) {
		    main.java.FSMDE.models.Diagram diagram = (main.java.FSMDE.models.Diagram) node
			    .getUserObject();
		    main.java.FSMDE.models.Diagram diagram2 = (main.java.FSMDE.models.Diagram) ((main.java.FSMDE.infrastructure.visual.DiagramTreeNode) path
			    .getLastPathComponent()).getUserObject();
		    if (diagram.getName() == diagram2.getName()) {
			setForeground(Color.red);
		    }
		}
		return (this);

	    }
	});
    }
}
